﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using QxFramework.Utilities;

// Cartoon FX  - (c) 2013, Jean Moreno

// Script handling the Demo scene of CartoonFX particles.

public class EffectManager : MonoSingleton<EffectManager>
{
    public bool orderedSpawns = true;
    public float step = 1.0f;
    public float range = 5.0f;
    private float order = -5.0f;

    public GameObject[] ParticleExamples;

    private Dictionary<string, float> ParticlesYOffsetD = new Dictionary<string, float>
    {
        {"CFX_ElectricGround", 0.15f},
        {"CFX_ElectricityBall", 1.0f},
        {"CFX_ElectricityBolt", 1.0f},
        {"CFX_Explosion", 2.0f},
        {"CFX_SmallExplosion", 1.5f},
        {"CFX_SmokeExplosion", 2.5f},
        {"CFX_Flame", 1.0f},
        {"CFX_DoubleFlame", 1.0f},
        {"CFX_Hit", 1.0f},
        {"CFX_CircularLightWall", 0.05f},
        {"CFX_LightWall", 0.05f},
        {"CFX_Flash", 2.0f},
        {"CFX_Poof", 1.5f},
        {"CFX_Virus", 1.0f},
        {"CFX_SmokePuffs", 2.0f},
        {"CFX_Slash", 1.0f},
        {"CFX_Splash", 0.05f},
        {"CFX_Fountain", 0.05f},
        {"CFX_Ripple", 0.05f},
        {"CFX_Magic", 2.0f},
        {"CFX_SoftStar", 1.0f},
        {"CFX_SpikyAura_Sphere", 1.0f},
        {"CFX_Firework", 2.4f},
        {"CFX_GroundA", 0.05f},
    };

    private int exampleIndex;
    private string randomSpawnsDelay = "0.5";
    private bool randomSpawns;

    private bool slowMo;


    public GameObject spawnParticle(int index)
    {
        GameObject particles = (GameObject)Instantiate(ParticleExamples[index]);

        particles.SetActive(true);
        for (int i = 0; i < particles.transform.childCount; i++)
            particles.transform.GetChild(i).gameObject.SetActive(true);


        float Y = 0.0f;
        foreach (KeyValuePair<string, float> kvp in ParticlesYOffsetD)
        {
            if (particles.name.StartsWith(kvp.Key))
            {
                Y = kvp.Value;
                break;
            }
        }
        particles.transform.position = new Vector3(0, Y, 0);

        return particles;
    }


    IEnumerator RandomSpawnsCoroutine()
    {

        LOOP:
        GameObject particles = spawnParticle(0);

        if (orderedSpawns)
        {
            particles.transform.position = this.transform.position + new Vector3(order, particles.transform.position.y, 0);
            order -= step;
            if (order < -range) order = range;
        }
        else particles.transform.position = this.transform.position + new Vector3(Random.Range(-range, range), 0, Random.Range(-range, range)) + new Vector3(0, particles.transform.position.y, 0);

        yield return new WaitForSeconds(float.Parse(randomSpawnsDelay));

        goto LOOP;
    }

}
